package com.payment.ktb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.constants.ConstantsUpdateArgs;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.AdvertisementEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.DeviceUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.LogUtil;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by syd on 2016/6/22.
 */
public class SplashActivity extends BaseActivity {

    LinearLayout mLinearLayout;
    ImageView mImageView;
    TranslateAnimation animation;
    TranslateAnimation animation1;
    ImageView iv_splash_prepic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        /**
         * 做一些动画效果
         */
        mLinearLayout = (LinearLayout)findViewById(R.id.ll_start_picture);
        mImageView = (ImageView)findViewById(R.id.iv_start_text);
        iv_splash_prepic = (ImageView)findViewById(R.id.iv_splash_prepic);


        int width = DeviceUtils.getDisplayMetrics().widthPixels;
        int height = DeviceUtils.getDisplayMetrics().heightPixels;
        mLinearLayout.getLayoutParams().width = width*3/2;
        mImageView.getLayoutParams().width = width;
        animation = new TranslateAnimation(0, -200, 0, 0);
        animation1 = new TranslateAnimation(0, 200, 0, 0);
        animation.setDuration(3000);
        animation1.setDuration(3000);
        animation.setFillAfter(true);
        animation1.setFillAfter(true);
        mLinearLayout.setAnimation(animation);
        mImageView.setAnimation(animation1);
        getStartAdInfo();

    }


    public void getStartAdInfo() {


        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        map.put("adArea","A1001");
        map.put("adSize","1");

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.AdInfoState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    List<AdvertisementEntity> listDatas = new Gson().fromJson(jsonObject.getJSONArray("adInfoList").toString(), new TypeToken<List<AdvertisementEntity>>() {}.getType());
                    if (listDatas != null && !listDatas.isEmpty()) {
                        Picasso.with(mContext).load(listDatas.get(0).getAreaPic()).into(iv_splash_prepic);
                    }
                    getUpdateInfo(listDatas);

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                startActivity(MainActivity.class);
                finish();
            }
        });
    }

    //自动升级
    public void getUpdateInfo(final List<AdvertisementEntity> listDatas) {


        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        map.put("os","ANDROID");
        map.put("version",DeviceUtils.getVersionCode() + "");

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.AppVersionState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    ConstantsUpdateArgs.isUpd = jsonObject.optString("isUpd");
                    ConstantsUpdateArgs.updateFlg = jsonObject.optString("updateFlg");
                    ConstantsUpdateArgs.updateTitle = jsonObject.optString("updateTitle");
                    ConstantsUpdateArgs.updateMsg = jsonObject.optString("updateMsg");
                    ConstantsUpdateArgs.downloadUrl = jsonObject.optString("downloadUrl");

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (listDatas == null || listDatas.isEmpty()) {
                                startActivityThenKill(MainActivity.class);
                                finish();
                            }else {
                                Intent intent = new Intent(mContext,StartAdvertisementActivity.class);
                                intent.putExtra("duration",listDatas.get(0).getDuration());
                                intent.putExtra("areaPic",listDatas.get(0).getAreaPic());
                                intent.putExtra("areaHref",listDatas.get(0).getAreaHref());
                                intent.putExtra("jumpFlg",listDatas.get(0).getJumpFlg());
                                startActivity(intent);
                                finish();
                            }
                        }
                    },3000);


                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                startActivity(MainActivity.class);
                finish();
            }
        });

    }
}
